package com.learning.platform.service;

import com.learning.platform.dto.QuestionBankRequest;
import com.learning.platform.dto.QuestionBankResponse;
import com.learning.platform.entity.QuestionBank;
import com.learning.platform.enums.Difficulty;
import com.learning.platform.enums.QuestionType;
import com.learning.platform.exception.BusinessException;
import com.learning.platform.repository.QuestionBankRepository;
import com.learning.platform.security.UserPrincipal;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class QuestionBankService {
    
    private final QuestionBankRepository questionBankRepository;
    
    public QuestionBankService(QuestionBankRepository questionBankRepository) {
        this.questionBankRepository = questionBankRepository;
    }
    
    /**
     * 创建题目
     */
    public QuestionBankResponse createQuestion(QuestionBankRequest request, UserPrincipal currentUser) {
        QuestionBank questionBank = new QuestionBank();
        questionBank.setCourseId(request.getCourseId());
        questionBank.setTitle(request.getTitle());
        questionBank.setContent(request.getContent());
        questionBank.setQuestionType(request.getQuestionType());
        questionBank.setOptions(request.getOptions());
        questionBank.setCorrectAnswers(request.getCorrectAnswers());
        questionBank.setExplanation(request.getExplanation());
        questionBank.setDifficulty(request.getDifficulty());
        questionBank.setPoints(request.getPoints());
        questionBank.setCreatedBy(currentUser.getUserId());
        
        QuestionBank saved = questionBankRepository.save(questionBank);
        return new QuestionBankResponse(saved);
    }
    
    /**
     * 更新题目
     */
    public QuestionBankResponse updateQuestion(Long id, QuestionBankRequest request, UserPrincipal currentUser) {
        QuestionBank questionBank = questionBankRepository.findById(id)
                .orElseThrow(() -> new BusinessException("题目不存在"));
        
        // 检查权限
        if (!questionBank.getCreatedBy().equals(currentUser.getUserId())) {
            throw new BusinessException("无权修改此题目");
        }
        
        questionBank.setTitle(request.getTitle());
        questionBank.setContent(request.getContent());
        questionBank.setQuestionType(request.getQuestionType());
        questionBank.setOptions(request.getOptions());
        questionBank.setCorrectAnswers(request.getCorrectAnswers());
        questionBank.setExplanation(request.getExplanation());
        questionBank.setDifficulty(request.getDifficulty());
        questionBank.setPoints(request.getPoints());
        
        QuestionBank saved = questionBankRepository.save(questionBank);
        return new QuestionBankResponse(saved);
    }
    
    /**
     * 删除题目
     */
    public void deleteQuestion(Long id, UserPrincipal currentUser) {
        QuestionBank questionBank = questionBankRepository.findById(id)
                .orElseThrow(() -> new BusinessException("题目不存在"));
        
        // 检查权限
        if (!questionBank.getCreatedBy().equals(currentUser.getUserId())) {
            throw new BusinessException("无权删除此题目");
        }
        
        questionBankRepository.delete(questionBank);
    }
    
    /**
     * 获取题目详情
     */
    @Transactional(readOnly = true)
    public QuestionBankResponse getQuestion(Long id) {
        QuestionBank questionBank = questionBankRepository.findById(id)
                .orElseThrow(() -> new BusinessException("题目不存在"));
        
        return new QuestionBankResponse(questionBank);
    }
    
    /**
     * 分页查询课程题目
     */
    @Transactional(readOnly = true)
    public Page<QuestionBankResponse> getQuestionsByCourse(Long courseId, QuestionType questionType, 
                                                         Difficulty difficulty, String keyword, Pageable pageable) {
        return questionBankRepository.findByCourseId(courseId, questionType, difficulty, keyword, pageable)
                .map(QuestionBankResponse::new);
    }
    
    /**
     * 搜索题目
     */
    @Transactional(readOnly = true)
    public Page<QuestionBankResponse> searchQuestions(Long courseId, QuestionType questionType, String difficulty, String keyword, Pageable pageable) {
        return questionBankRepository.searchQuestions(courseId, questionType, difficulty, keyword, pageable)
                .map(QuestionBankResponse::new);
    }
    
    /**
     * 统计课程题目数量
     */
    @Transactional(readOnly = true)
    public long countByCourse(Long courseId) {
        return questionBankRepository.countByCourseId(courseId);
    }
}
